Code execution with MCP: building more efficient AI agents
from Engineering at Anthropic: Inside the team building reliable AI systems
Code execution with MCP: building more efficient AI agents
公開日:2025年11月4日
Model Context Protocolの課題 
ツールからの過剰なトークン消費
中間ツールの結果生成には追加のトークンが必要
https://scrapbox.io/files/69116ddf5e14df5a672020c2.png
コードAPIとして提示する
MCPサーバから利用可能なツールを、ファイルツリーにする
code:tree
servers
├── google-drive
│ ├── getDocument.ts
│ ├── ... (other tools)
│ └── index.ts
├── salesforce
│ ├── updateRecord.ts
│ ├── ... (other tools)
│ └── index.ts
└── ... (other servers)
code:ts
// ./servers/google-drive/getDocument.ts
import { callMCPTool } from "../../../client.js";
interface GetDocumentInput {
documentId: string;
}
interface GetDocumentResponse {
content: string;
}
/* Read a document from Google Drive */
export async function getDocument(input: GetDocumentInput): Promise<GetDocumentResponse> {
return callMCPTool<GetDocumentResponse>('google_drive__get_document', input);
}
callMCPToolを叩けば実行できるようにしてある
requestとresponseをtypeにする
descriptionはどこだ?miyamonz.icon
エージェントはファイルシステムを探索することで利用可能なツールを発見します。
./servers/ディレクトリを一覧表示して接続可能なサーバー(google-driveやsalesforceなど)を特定し、
その後各ツールのインターフェースを理解するために必要な個別のツールファイル(getDocument.tsやupdateRecord.tsなど)を読み込みます。
これにより、エージェントは現在のタスクに必要な定義情報のみをロードできるようになります。これにより、トークン使用量が15万トークンから2,000トークンに削減され、時間とコストが98.7%節約されます。
これはAgent Skillsだねmiyamonz.icon
Cloudflare もCode Modeと呼んでる
Code Mode: the better way to use MCP
制御フローを素直にコードで書ける
プライバシー保護の観点
状態の保持
clientのよびだし
https://chatgpt.com/c/69117039-ec40-8320-8bac-f956c7a5200f